var searchItems = null;
var currentUserId = null;

var userRequest = null;
var now = new Date();
var clickedBuzz = null;

var searchTerm = null;

function init()
{
	onResize();
}
function dismissSplashScreen()
{
	gotoScreen('home_screen', false, true);
}
function searchClicked()
{
	var term = $('buzz_term').value;
	
	if(term.length == 0)
	{
		showError('Please enter a term');
	}
	else
	{
		$('home_loader').style.display = '';
		$('home_container').innerHTML = '';
	
		gotoScreen('home_screen');
		
		searchTerm = term;
		
		if($('buzz_gps').checked)
		{
			getDeviceLocation(searchGetLocationHandler);
		}
		else
		{
			searchBuzzes();
		}
	}
}
function searchGetLocationHandler(returnValue, errorCode, errorMessage)
{
	if(errorCode == 0)
	{
		searchBuzzes(returnValue.Latitude, returnValue.Longitude);
	}
	else
	{
		showError(errorMessage);
	}
}
function searchBuzzes(latitude, longitude)
{
	var searchURL = 'https://www.googleapis.com/buzz/v1/activities/search?alt=json&q=' + encodeURIComponent(searchTerm);
	
	if(latitude && longitude)
	{
		searchURL += '&lat=' + latitude + '&lon=' + longitude + '&radius=100000';
	}
	loadBuzzes(searchURL, buzzesHandler);
}
function buzzesHandler(success, buzzes)
{
	var res = '';
	
	$('home_loader').style.display = 'none';
	
	if(success)
	{
		searchItems = buzzes;
		
		if(buzzes && buzzes.length > 0)
		{
			var item;
			
			for(var i = 0; i < buzzes.length; i++)
			{
				item = buzzes[i];
				
				res += '<div onclick="clickSearchBuzz(' + i + ')" class="item buzz">' + 
					'<img class="item_thumb" src="' + item.actor.thumbnailUrl + '" />' + 
					'<div class="item_title">' + item.actor.name + ' - ' +
					getDate(item.published) + 
					'</div>' + 
					'<div class="item_text">' + item.title + '</div>' + 
					'<div class="clearer"></div>' + 
					'</div>';
			}
		}
		else
		{
			res = '<div class="error">No Buzzes available, please try with another term</div>';
		}
	}
	else
	{
		res = '<div class="error">There was an error while searching Buzzes, please go back and try again</div>';
	}
	$('home_container').innerHTML = res;
	
	dismissSplashScreen();
}
function clickSearchBuzz(index)
{
	clickedBuzz = searchItems[index];
	
	viewUser(clickedBuzz.actor.id);
}
function viewUser(userId)
{
	$('user_loader').style.display = '';
	
	gotoScreen('user_screen');
	
	userRequest = loadBuzzes('https://www.googleapis.com/buzz/v1/activities/' + userId + '/@public?alt=json', userBuzzesHandler);
}
function userBuzzesHandler(success, buzzes, req)
{
	if(req == userRequest)
	{
		$('user_loader').style.display = 'none';
	
		var res = '';
		
		if(success)
		{
			var results = buzzes;
			
			if(results.length > 0)
			{
				var item;
				
				for(var i = 0; i < results.length; i++)
				{
					item = results[i];
					
					res += '<div class="item">' + 
						'<img class="item_thumb" src="' + item.actor.thumbnailUrl + '" />' + 
						'<div class="item_text">' + item.title + '</div>' + 
						'<div class="clearer"></div>' + 
						'</div>';
				}
			}
			else
			{
				res = '<div class="error">No Buzzes available</div>';
			}
		}
		else
		{
			res = '<div class="error">No Buzzes available</div>';
		}
		$('user_container').innerHTML = res;
		
		userRequest = null;
	}
}

